﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Simple.Service;
using Simple.Web.Framework.Infrastructure.Aop;
using Simple.ViewModel;
using Simple.Common.Pager;
using Simple.Web.Framework.Infrastructure;
using Simple.Common.EasyUICommon;
using Simple.Entity.Enums;

namespace HBootStrap.Admin.Controllers
{
    public class DataDictionaryController : BaseController
    {
        private DataDictionaryService _dataDictionaryService;
        public DataDictionaryController(DataDictionaryService dataDictionaryService)
        {
            _dataDictionaryService = dataDictionaryService;
        }

        [MenuViewFilter(MenuName = "字典管理", ModularName = "基础信息管理", Sort = 3)]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AjaxIndex(SimpleMvcPage mvcPage, DataDictionaryQueryModel queryModel)
        {
            var model = _dataDictionaryService.GetPageList(mvcPage, queryModel);
            return Json(new PageDataGrid { rows = model, total = model.TotalCount }, JsonRequestBehavior.AllowGet);
        }

        [MenuViewFilter(MenuName = "字典分类管理", ModularName = "基础信息管理", Sort = 3)]
        public ActionResult ParentIndex()
        {
            return View();
        }

        public ActionResult AjaxParentIndex(SimpleMvcPage mvcPage, DataDictionaryQueryModel queryModel)
        {
            var model = _dataDictionaryService.GetParentPageList(mvcPage, queryModel);
            return Json(new PageDataGrid { rows = model, total = model.TotalCount }, JsonRequestBehavior.AllowGet);
        }

        [ActionViewFilter(ActionName = "添加字典分类", MenuName = "字典分类管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        public ActionResult AddParentDictionary()
        {
            DataDictionaryDto model = new DataDictionaryDto();
            model.Sort = 0;
            model.IsActive = true;
            model.ParentId = 0;
            model.Code = "#";
            return View(model);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加字典分类", MenuName = "字典分类管理", Sort = 1, ActionType = ActionTypeEnum.Add)]
        public ActionResult AddParentDictionary(DataDictionaryDto dto)
        {
            var result = _dataDictionaryService.AddParent(dto.ToEntity());
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("ParentIndex", "DataDictionary");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "编辑字典分类", MenuName = "字典分类管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        public ActionResult EditParentDictionary(long id)
        {
            var dto = _dataDictionaryService.FindById(id).ToDto();
            return View(dto);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "编辑字典分类", MenuName = "字典分类管理", Sort = 2, ActionType = ActionTypeEnum.Edit)]
        public ActionResult EditParentDictionary(DataDictionaryDto dto)
        {
            var updateModel = _dataDictionaryService.FindById(dto.Id);
            updateModel.Title = dto.Title;
            var result = _dataDictionaryService.UpdateParent(updateModel);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("ParentIndex", "DataDictionary");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "添加字典", MenuName = "字典管理", Sort = 3, ActionType = ActionTypeEnum.Add)]
        public ActionResult Add(long? id)
        {
            DataDictionaryDto model = new DataDictionaryDto();
            model.Sort = 0;
            model.IsActive = true;
            model.ParentId = id ?? 0;
            model.ParentDictionaryList = _cacheService.GetParentDataDictionaryList(id);
            return View(model);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "添加字典", MenuName = "字典管理", Sort = 3, ActionType = ActionTypeEnum.Add)]
        public ActionResult Add(DataDictionaryDto dto)
        {
            var result = _dataDictionaryService.Add(dto.ToEntity());
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "DataDictionary");
            }
            return Json(result);
        }

        [ActionViewFilter(ActionName = "编辑字典", MenuName = "字典管理", Sort = 4, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(long id)
        {
            var model = _dataDictionaryService.FindById(id);
            var dto = model.ToDto();
            dto.ParentDictionaryList = _cacheService.GetParentDataDictionaryList(dto.ParentId);
            return View(dto);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "编辑字典", MenuName = "字典管理", Sort = 4, ActionType = ActionTypeEnum.Edit)]
        public ActionResult Edit(DataDictionaryDto dto)
        {
            var updateModel = _dataDictionaryService.FindById(dto.Id);
            updateModel.Title = dto.Title;
            updateModel.Code = dto.Code;
            updateModel.Sort = dto.Sort;
            var result = _dataDictionaryService.Update(updateModel);
            if (result.statusCode == SimpleResultCode.Success)
            {
                result.redirectUrl = Url.Action("Index", "DataDictionary");
            }
            return Json(result);
        }

        [HttpPost]
        [ActionViewFilter(ActionName = "删除字典", MenuName = "字典管理", Sort = 5, ActionType = ActionTypeEnum.Delete)]
        public ActionResult Delete(long id)
        {
            var result = _dataDictionaryService.Delete(id);
            return Json(result);
        }

    }
}
